#include <iostream>
#include <map>
using namespace std;

const int kMaxN = 2e5 + 10;

int a[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n, k, l, r;

    cin >> n >> k >> l >> r;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    a[n] = -1;
    int p = 0, q = 0;
    long long ans = 0;
    map<int, int> pm, qm;

    for (int i = 0; i < n; i++) {
      for (; pm.size() < k && p < n; p++) {
        pm[a[p]]++;
      }
      for (; qm.size() <= k && q <= n; q++) {
        qm[a[q]]++;
      }
      if (pm.size() == k) {
        int len1 = p - i;
        int len2 = q - i - 1;
        ans += max(0, min(len2, r) - max(l, len1) + 1);
      }
      pm[a[i]]--;
      if (pm[a[i]] == 0) {
        pm.erase(a[i]);
      }
      qm[a[i]]--;
      if (qm[a[i]] == 0) {
        qm.erase(a[i]);
      }
    }
    cout << ans << "\n";
  }
}